package com.yyk.config.commonService.response;

import com.yyk.config.commonService.enumeration.HttpStatusEnum;

import java.io.Serializable;

/**
 * @author ljp
 * @CLASSNAME YYKCloudResponse
 * @date 2020/12/2 10:23
 * @description： web端 统一返回包
 */

public final class YYKCloudResponse<T> implements Serializable {
    private static final long serialVersionUID = 1L;

    /**
     * 状态码
     */
    private Integer code;

    /**
     * 信息
     */
    private String message;

    /**
     * 相应数据
     */
    private T data;

    public YYKCloudResponse() {
    }

    public YYKCloudResponse(Integer code, String message) {
        this.code = code;
        this.message = message;
    }

    public YYKCloudResponse(Integer code, String message, T data) {
        this.code = code;
        this.message = message;
        this.data = data;
    }

    /**
     * 返回错误信息
     *
     * @return
     */
    public static <T> YYKCloudResponse<T> error(Integer status, String msg) {
        return new YYKCloudResponse<T>(status, msg);
    }


    /**
     * 返回错误信息,如果是只有信息则是自定义业务异常
     * @return
     */
    public static <T> YYKCloudResponse<T> error(String msg) {
        return new YYKCloudResponse<T>(HttpStatusEnum.HTTP_AUTO_RUNTIME.getCode(), msg);
    }


    /**
     * 直接返回成功
     *
     * @return
     */
    public static <T> YYKCloudResponse<T> success() {
        return new YYKCloudResponse<T>(HttpStatusEnum.HTTP_OK.getCode(), HttpStatusEnum.HTTP_OK.getDesc());
    }

    /**
     * 返回成功信息 此方法将传入参数装到result中，因此只有返回业务数据时调用此方法
     *
     * @return
     */
    public static <T> YYKCloudResponse<T> success(T result) {
        return new YYKCloudResponse<T>(HttpStatusEnum.HTTP_OK.getCode(), HttpStatusEnum.HTTP_OK.getDesc(), result);
    }


    public Integer getCode() {
        return code;
    }

    public void setCode(Integer code) {
        this.code = code;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String message) {
        this.message = message;
    }

    public T getData() {
        return data;
    }

    public void setData(T data) {
        this.data = data;
    }
}
